package com.jeady.timetodo.activities.account

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jeady.timetodo.ui.theme.TimeToDoTheme

class AccountActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TimeToDoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize()
                ) {
                    AccountBookScreen()
                }
            }
        }
    }
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AccountBookScreen() {
    val entries = remember { mutableStateListOf<Entry>() }
    var newEntryDescription by remember { mutableStateOf(TextFieldValue("")) }
    var newEntryAmount by remember { mutableStateOf(TextFieldValue("")) }

    Scaffold(
        Modifier.fillMaxSize(),
        content = {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier
                    .padding(it)
                    .fillMaxWidth()
            ) {
                Spacer(modifier = Modifier.height(16.dp))

                // Displaying the list of entries
                LazyColumn(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                     items(entries) { entry ->
                        Card(
                            modifier = Modifier.padding(8.dp),
                            shape = MaterialTheme.shapes.medium,
                        ) {
                            Column(
                                horizontalAlignment = Alignment.CenterHorizontally,
                                verticalArrangement = Arrangement.Center,
                                modifier = Modifier.padding(16.dp)
                            ) {
                                Text(entry.description)
                                Spacer(modifier = Modifier.height(8.dp))
                                Text("Amount: ${entry.amount}")
                            }
                        }
                    }
                }

                Spacer(modifier = Modifier.height(16.dp))

                // Adding a new entry
                Card(
                    modifier = Modifier.padding(8.dp),
                    shape = MaterialTheme.shapes.medium,
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Center,
                        modifier = Modifier.padding(16.dp)
                    ) {
                        Text("New Entry")
                        Spacer(modifier = Modifier.height(8.dp))

                        TextField(
                            value = newEntryDescription,
                            onValueChange = { newEntryDescription = it },
                            label = { Text("Description") },
                            keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Text)
                        )

                        Spacer(modifier = Modifier.height(8.dp))

                        TextField(
                            value = newEntryAmount,
                            onValueChange = { newEntryAmount = it },
                            label = { Text("Amount") },
                            keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)
                        )

                        Spacer(modifier = Modifier.height(8.dp))

                        Button(
                            onClick = {
                                entries.add(Entry(newEntryDescription.text, newEntryAmount.text.toDouble()))
                                newEntryDescription = TextFieldValue("")
                                newEntryAmount = TextFieldValue("")
                            }
                        ) {
                            Text("Add")
                        }
                    }
                }
            }
        }
    )
}

data class Entry(val description: String, val amount: Double)

@Preview(showBackground = true)
@Composable
private fun AccountPreview() {
    AccountBookScreen()
}